package interpreter_pattern.ex2;

/**
 * 客户端类，负责构建解释器树并进行解释。
 * @author 申书航
 * @version 1.0
 */
public class Client {

    /**
     * 构建解释器树，返回一个表达式对象。
     * 该树的结构为：((three or (one or two)) and four)
     * @return 返回构建好的表达式
     */
    public Expression buildInterpreterTree() {
        Expression one = new TerminalExpression("one");
        Expression two = new TerminalExpression("two");
        Expression three = new TerminalExpression("three");
        Expression four = new TerminalExpression("four");
        // or1 = (one or two)
        Expression or1 = new OrExpression(one, two);
        // or2 = (three or (one or two))
        Expression or2 = new OrExpression(three, or1);
        // and = ((three or (one or two)) and four)
        Expression and = new AndExpression(or2, four);
        return and;
    }

    /**
     * 主程序入口，创建客户端并执行表达式解释。
     * @param args 命令行参数
     */
    public static void main(String[] args) {
         Client client = new Client();
         Expression expression = client.buildInterpreterTree();
         String ctx = "one two aaa aaa";
         System.out.println(expression.interpret(ctx));
    }
}

